import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/base_productionstation.dart';
import 'package:flutter_admin/utils/store_util.dart';
import '../../../../generated/l10n.dart';

class BaseProductionstationDialog extends StatefulWidget {
  BaseProductionstationDialog();

  @override
  _BaseProductionstationDialogState createState() => _BaseProductionstationDialogState();
}

class _BaseProductionstationDialogState extends State<BaseProductionstationDialog> {

  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  BaseProductionstation  baseProductionstation = BaseProductionstation();
  //车间名称
  List<SelectOptionVO> workshopIdList = [];
  //工厂名称
  List<SelectOptionVO> factoryIdList = [];

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      workshopIdList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "!=",
          val: null);
    }else{
      baseProductionstation.workshopId = userInfo.deptId;
    }

    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          userInfo.major != null
              ? CrySelect(
            label: '车间名称',
            dataList: workshopIdList,
            value: baseProductionstation.workshopId,
            onSaved: (v) => {baseProductionstation.workshopId = v},
          )
              : CryInput(
            label: '生产车间',
            enable: false,
            value: userInfo.deptName,
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, baseProductionstation);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}